package com.example.blank700.datepicker_dialog;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.view.View.OnClickListener;
import android.app.DatePickerDialog;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import android.widget.Toast;

public class DatePicker_Dialog extends AppCompatActivity {
    private TextView showdate;
    private Button setdate;
    private int year;
    private int month;
    private int day;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_date_picker_dialog);

        showdate = (TextView) this.findViewById(R.id.showtime);
        setdate = (Button) this.findViewById(R.id.setdate);

        Calendar calendar = Calendar.getInstance(Locale.CANADA);
        final Date date = new Date();
        calendar.setTime(date);

        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH);
        day = calendar.get(Calendar.DAY_OF_MONTH);
        showdate.setText("CurrentDate:" + year + "-" + (month + 1) + "-" + day);

        setdate.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                DatePickerDialog datePickerDialog = new DatePickerDialog(DatePicker_Dialog.this, Datelistener, year, month, day);
                datePickerDialog.show();
            }
        });
    }

    private DatePickerDialog.OnDateSetListener Datelistener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int y, int m, int dayOfMonth) {
            Date currentdate = new Date();
            //if((y<currentdate.getYear())&&(m<currentdate.getMonth())&&(dayOfMonth<currentdate.getDay()))
            year = y;
            month = m;
            day = dayOfMonth;
            Toast.makeText(DatePicker_Dialog.this, "Date is update:" + "CurrentDate:" + year + "-" + (month + 1) + "-" + day, Toast.LENGTH_SHORT).show();
            updateDate();
        }

        private void updateDate() {
            showdate.setText("CurrentDate:" + year + "-" + (month + 1) + "-" + day);
        }
    };
}
